import { getFolderList,
  fullBackup,
  incrementalBackup,
  restoreBackup
 } from '@/api/server/backup'
import { ref } from 'vue'

export function useBackup() {

  const dirPath = ref('')
  const folderList = ref([])
 
  // 全量备份
  const fullBackupDialogVisible = ref(false)
  const fullBackFlag = ref(false)
  const openFullBackupDialog =async () => {
    fullBackFlag.value = false
    dirPath.value = ''
    const res = await getFolderList({path:dirPath.value})
    folderList.value = res.data.data
    fullBackupDialogVisible.value = true
  }

  

  // 全量备份 输入密码对话框
  const fullBackupPasswordDialogVisible = ref(false)
  const fullBackupPassword = ref('')  
  const openFullBackupPasswordDialog = () => {
    fullBackupPassword.value = ''
    if(dirPath.value === ''){
      ElMessage.error('请选择备份路径')
      return
    }
    fullBackupPasswordDialogVisible.value = true
  }

  // 输入密码后， 全量备份
  const fullBackupOfHook =async () => {
    
    if(fullBackupPassword.value === ''){
      ElMessage.error('请输入密码')
      return
    }

    if(fullBackFlag.value){
      const res = await fullBackup({storagePath:dirPath.value,password:fullBackupPassword.value})
      if(parseInt(res.data.code) === 200){
        ElMessage.success('全量备份成功')
        fullBackupDialogVisible.value = false
        fullBackupPasswordDialogVisible.value = false
      }else{
        ElMessage.error('全量备份失败')
      }
    }
    
  }



  // 增量备份
  const incrementalBackupDialogVisible = ref(false)
  const incrementalBackupPasswordDialogVisible = ref(false)
  const incrementalBackupPassword = ref('')
  const incrementalBackupFlag = ref(false)
  const openIncrementalBackupDialog =async () => {

    incrementalBackupFlag.value = false
    dirPath.value = ''
    const res = await getFolderList({path:dirPath.value})
    folderList.value = res.data.data
    incrementalBackupDialogVisible.value = true
  }

  const openIncrementalBackupPasswordDialog = () => {

    incrementalBackupPassword.value = ''
    if(dirPath.value === ''){
      ElMessage.error('请选择备份路径')
      return
    }
    incrementalBackupPasswordDialogVisible.value = true
  }
  

    // 输入密码后， 增量备份
    const incrementalBackupOfHook =async () => {
    
      if(incrementalBackupPassword.value === ''){
        ElMessage.error('请输入密码')
        return
      }
  
      if(incrementalBackupFlag.value){
    
        const res = await incrementalBackup({storagePath:dirPath.value,password:incrementalBackupPassword.value})
        if(parseInt(res.data.code) === 200){
          ElMessage.success('增量备份成功')
          incrementalBackupDialogVisible.value = false
          incrementalBackupPasswordDialogVisible.value = false
        }else{
          ElMessage.error('增量备份失败')
        }
      }
      
    }
  

  const getFolderListOfHook = async (path,back = false) => {
    if(back){
      dirPath.value = path
    }else{
      if(dirPath.value === ''){
        dirPath.value = path
      }else{
        dirPath.value += `\\\\${path}`  
      }
    }


    const res = await getFolderList({path:dirPath.value})
    folderList.value = res.data.data
  }


  const backPath = () => {
    if( typeof dirPath.value === 'string' && dirPath.value.includes('\\\\')){
      dirPath.value = dirPath.value.substring(0, dirPath.value.lastIndexOf('\\\\'))
      getFolderListOfHook(dirPath.value,true)
    }else{
      getFolderListOfHook('',true)
    }
   
  }


  // 还原备份数据
  const restoreBackupDialogVisible = ref(false)
  const restoreBackupPassword = ref('')
  const selectedFile = ref(null)
  const openRestoreBackupDialog = () => {
    restoreBackupPassword.value = ''
    selectedFile.value = null
    restoreBackupDialogVisible.value = true
  } 

// 开始上传
const startUploadOfHook = async () => {
  if (!selectedFile.value) {
    ElMessage.error('请先选择备份文件')
    return
  }

  const formData = new FormData()
  formData.append('password', restoreBackupPassword.value)
  formData.append('backup', selectedFile.value)

  try {
    const response = await restoreBackup(formData)
    if(parseInt(response.data.code) === 200){
      restoreBackupDialogVisible.value = false
      ElMessage.success('还原备份数据成功')
      return true;
    }else{
      ElMessage.error(`${response.data.message}`)
      return false;
    }

  } catch (error) {
    ElMessage.error(`密码错误，无法解压!`)
    return false;
  } 
}



  return {
    folderList,
    dirPath,

    fullBackupDialogVisible,
    openFullBackupDialog,
    getFolderListOfHook,
    backPath,

    fullBackupOfHook,

    fullBackupPasswordDialogVisible,
    fullBackupPassword,
    fullBackFlag,
    openFullBackupPasswordDialog,

    // 增量备份
    incrementalBackupDialogVisible,
    incrementalBackupPasswordDialogVisible,
    incrementalBackupPassword,
    incrementalBackupFlag,
    openIncrementalBackupDialog,
    openIncrementalBackupPasswordDialog,
    incrementalBackupOfHook,

    // 还原备份数据
    restoreBackupDialogVisible,
    restoreBackupPassword,
    selectedFile,
    openRestoreBackupDialog,
    startUploadOfHook
  }
}
